/**
 * 史诗钥匙地下城
 */
const Router = require('@koa/router');
const {
  MythicKeystoneDungeonsIndex: MythicDungeonIndex,
  MythicKeystoneDungeon: MythicDungeon
} = require('../db/schemas')

const { Language } = require('../env');
const { genResponse, raw$project } = require('../utils');

const router = new Router({ prefix: '/mythic-dungeon' })

router.get('/index', async (ctx) => {
  const results = await MythicDungeonIndex.aggregate([
    {
      $project: {
        dungeons: {
          $map: {
            input: '$dungeons',
            as: 'd',
            in: {
              id: '$$d.id',
              name: `$$d.name.${Language}`
            }
          }
        },
      }
    },
  ])
  ctx.body = genResponse(true, results[0].dungeons)
})

router.get('/', async (ctx) => {
  const results = await MythicDungeon.aggregate([
    {
      $addFields: {
        journal_instance_id: '$dungeon.id'
      }
    },
    {
      $project: {
        _id: 0,
        id: 1,
        name: `$name.${Language}`,
        journal_instance_id: 1,
        // description: `$description.${Language}`,
        updated_at: 1,
        // raw: '$$ROOT'
      }
    },
    // raw$project,
  ])
  ctx.body = genResponse(true, results)
})

module.exports = router